package com.looso.sfolder.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import com.looso.sfolder.R;
import com.looso.sfolder.data.FileData;
import com.looso.sfolder.data.FileListData;
import com.looso.sfolder.data.FileRequestData;
import com.looso.sfolder.net.HttpsInterface;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class FileBrowserActivity extends Activity implements
		AdapterView.OnItemClickListener {

	private ListView mListView = null;
	private ScrollView mScrollView = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.filebrowser);
		
		mListView = (ListView) findViewById(R.id.filebrowser_list);
		mListView.setCacheColorHint(Color.TRANSPARENT);
		mListView.setBackgroundColor(Color.WHITE);
		mListView.setSelector(R.drawable.templatelist_item_bg);
		mListView.setOnItemClickListener(this);

		GetFilelistProgressDialog progressDialog = new GetFilelistProgressDialog(
				this);
		progressDialog.setMessage(getString(R.string.filebrowser_loading));
		progressDialog.show();
	}

	private FileListData responseData = null;
	private SimpleAdapter mAdapter = null;

	class GetFilelistProgressDialog extends LoadingProgressDialog {

		public GetFilelistProgressDialog(Context context) {
			super(context);
		}

		@Override
		protected void load() {
			// 1.get file list infos
			responseData = HttpsInterface.getFileList();
			if (responseData == null) {
				mUIHandler.sendEmptyMessage(error_filebrowser_getlist_error);
			} else {
				ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
				Iterator<FileRequestData> it = responseData
						.getFileRequestDatas().iterator();
				while (it.hasNext()) {
					FileRequestData d = it.next();
					HashMap<String, String> map = new HashMap<String, String>();
					map.put("text", d.getName());
					data.add(map);
				}
				mAdapter = new SimpleAdapter(
						FileBrowserActivity.this, data, R.layout.filelistitem,
						new String[] { "text" },
						new int[] { R.id.filelistitem_text });
//				mListView.setAdapter(adapter);
				mUIHandler.sendEmptyMessage(refresh_listview);

				// for test
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			dismiss();
		}

	}
	

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		FileRequestData data = responseData.getFileRequestDatas().get(position);
		GetFileInfoProgressDialog progressDialog = new GetFileInfoProgressDialog(FileBrowserActivity.this, data.getId());
		progressDialog.setMessage(getString(R.string.file_loading));
		progressDialog.show();
	}

	
	private FileData mFileData = null;
	class GetFileInfoProgressDialog extends LoadingProgressDialog {
		private String id = null;
		public GetFileInfoProgressDialog(Context context,String id) {
			super(context);
			this.id = id;
		}
		@Override
		protected void load() {
			// 1.get file list infos
			mFileData = HttpsInterface.getFile(id);
			if (mFileData == null) {
				mUIHandler.sendEmptyMessage(error_filebrowser_getfile_error);
			} else {
				Intent intent = new Intent();
				intent.setClass(FileBrowserActivity.this, ImagesetReaderActivity.class);
				
				intent.putExtra("data", mFileData);
				startActivity(intent);

				// for test
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			dismiss();
		}
	}
	
	private static final int error_filebrowser_getfile_error = 1;
	private static final int error_filebrowser_getlist_error = 2;
	private static final int refresh_listview = 3;
	private UIHandler mUIHandler = new UIHandler();
	class UIHandler extends Handler{
		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case error_filebrowser_getfile_error:
				Toast.makeText(FileBrowserActivity.this,
						getString(R.string.error_filebrowser_getfile_error),
						Toast.LENGTH_SHORT).show();
				break;
			case error_filebrowser_getlist_error:
				Toast.makeText(FileBrowserActivity.this,
						getString(R.string.error_filebrowser_getlist_error),
						Toast.LENGTH_SHORT).show();
				break;
			case refresh_listview:
				if(mListView!=null && mAdapter!=null){
					mListView.setAdapter(mAdapter);
				}
				break;
			default:
				break;
			}
		}
	}
}
